package com.baomidou.ant.controller;


import com.baomidou.ant.common.Result;
import com.baomidou.ant.entity.SysDept;
import com.baomidou.ant.entity.SysUser;
import com.baomidou.ant.service.ISysDeptService;

import com.baomidou.ant.service.ISysUserService;
import com.baomidou.ant.vo.request.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jobob
 * @since 2022-03-18
 */
@RestController
@RequestMapping("/sys-dept")
public class SysDeptController {
    @Autowired
    private ISysDeptService sysDeptService;
    @Autowired
    private ISysUserService sysUserService;

    @PostMapping("/listDept")
    public Result listDept(){
        List<SysDept> sysDeptList=sysDeptService.list();
        /*List<ResponseDeptNameVo> voList=new ArrayList<>();
        for(SysDept sysDept:sysDeptList){
            ResponseDeptNameVo vo=new ResponseDeptNameVo();
            BeanUtils.copyProperties(sysDept,vo);
            voList.add(vo);
        }*/
        return Result.getSuccess().setData(sysDeptList);
    }

    @GetMapping("/getDeptNameByUser/{userId}")
    public Result getDeptNameByUser(@PathVariable Integer userId){
        SysUser user=sysUserService.getById(userId);
        if(Objects.equals(user.getType(), "1")){
            return Result.getFailure();
        }
        SysDept dept=sysDeptService.getById(user.getDeptId());
        String DeptName=dept.getDept();
        return Result.getSuccess().setData(DeptName);
    }

    @PostMapping("/list")
    public Result list(@RequestBody RequestListDeptVo requestListDeptVo){
        IPage<SysDept> deptIPage=sysDeptService.list(requestListDeptVo);
        return Result.getSuccess().setData(deptIPage);
    }

    @GetMapping("/getDept/{id}")
    public Result getDept(@PathVariable Integer id){
        SysDept sysDept=sysDeptService.getById(id);
        return Result.getSuccess().setData(sysDept);
    }

    @PostMapping("/update")
    public Result update(@RequestBody RequestUpdateDeptVo requestUpdateDeptVo){
        SysUser user=sysUserService.getById(requestUpdateDeptVo.getUserId());
        //如果不是管理员
        if (!Objects.equals(user.getType(), "1")){
            return Result.getFailure().setMsg("您不超级管理员，无权编辑部门信息");
        }
        if(sysDeptService.queryName(requestUpdateDeptVo.getDept())){
            return Result.getFailure().setMsg("存在该部门或没做任何更改！");
        }
        SysDept dept=sysDeptService.getById(requestUpdateDeptVo.getId());
        dept.setDept(requestUpdateDeptVo.getDept());
        if(sysDeptService.updateById(dept)){
            return Result.getSuccess().setMsg("保存编辑");
        }else
            return Result.getFailure().setMsg("编辑失败");
    }

    @PostMapping("/deleteDept")
    public Result deleteDept(@RequestBody RequestDeleteVo requestDeleteVo){
        return Result.getFailure().setMsg("禁止删除部门！");
//        SysUser user=sysUserService.getById(requestDeleteVo.getUserId());
//        //如果不是管理员
//        if (!Objects.equals(user.getType(), "1")){
//            return Result.getFailure().setMsg("您不是超级管理员，无权删除部门");
//        }
//        //如果是管理员
//        if(sysDeptService.removeById(requestDeleteVo.getGoalId())){
//            return Result.getSuccess().setMsg("删除成功");
//        }else{
//            return Result.getFailure().setMsg("删除失败");
//        }
    }

    //新增部门接口
    @PostMapping("/add")
    public Result register(@RequestBody RequestSaveDeptVo requestSaveDeptVo){
        //判断创建者权限
        SysUser createUser=sysUserService.getById(requestSaveDeptVo.getUserId());
        if(!Objects.equals(createUser.getType(), "1")){
            return Result.getFailure().setMsg("您无权添加部门,请联系超级管理员");
        }
        if(sysDeptService.queryName(requestSaveDeptVo.getDept())){
            return Result.getFailure().setMsg("存在该部门！");
        }
        SysDept sysDept=new SysDept();
        sysDept.setDept(requestSaveDeptVo.getDept());
        sysDeptService.save(sysDept);
        return Result.getSuccess().setMsg("添加成功");
    }

}
